/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.util;

import java.util.List;
import model.pojo.BaoChiViet;
import model.pojo.SuKienTinTuc;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author LaiHuynh
 */
public class BaoChiVietDAO {
    public static List<BaoChiViet> layDanhSachTinTuc() {
        List<BaoChiViet> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from BaoChiViet sk order by sk.ngayViet desc";
            org.hibernate.Query query = session.createQuery(hql);
            query.setFirstResult(0);
            query.setMaxResults(4);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static BaoChiViet layThongTinTinTuc(int maTinTuc) {
        BaoChiViet tt = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            tt = (BaoChiViet) session.get(BaoChiViet.class, maTinTuc);
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return tt;
    }

    public static List<BaoChiViet> LayDSPhanTrang(long slsach, long trang) {
        List<BaoChiViet> ds = null;
        org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
        String hql;
        int n = ((int) trang - 1) * (int) slsach;
        int m = (int) slsach;
        try {
            if (slsach != -1) {
                hql = "select sk from BaoChiViet sk order by sk.ngayViet desc";
                org.hibernate.Query query = session.createQuery(hql);
                query.setFirstResult(n);
                query.setMaxResults(m);
                ds = query.list();
            }
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static long SoLuongTin() {
        long sl = 0;
        org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select count(ws.maBaoChi) from BaoChiViet ws";
        org.hibernate.Query query = session.createQuery(hql);
        sl = (Long) query.uniqueResult();
        session.close();
        return sl;
    }

    public static long TinhSoTrang(long SLTinTrenTrang) {
        long sl = SoLuongTin();
        long s;
        if (SLTinTrenTrang != -1) {
            s = sl / SLTinTrenTrang;
            if (sl % SLTinTrenTrang != 0) {
                s++;
            }
        } else {
            s = 1;
        }
        return s;
    }
    public static List<BaoChiViet> layDanhSachBaoChiViet() {
        List<BaoChiViet> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from BaoChiViet sk order by sk.ngayViet desc";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    public static boolean ThemTinTuc(BaoChiViet tt) {
        boolean kq = true;
        org.hibernate.classic.Session ss = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = null;
        try {
            trans = ss.beginTransaction();
            ss.save(tt);
            trans.commit();
        } catch (HibernateException ex) {
            trans.rollback();
            System.err.println(ex);
            kq = false;
        } finally {
            ss.close();
        }
        return kq;
    }
    public static boolean xoaTinTuc(int maCuaHang) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        BaoChiViet ch = BaoChiVietDAO.layThongTinTinTuc(maCuaHang);
        if (ch == null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.delete(ch);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
   public static int LayMa() {

        int ma = 0;
        org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
        boolean tinhtrang = true;
        String hql = "select max(tin.maBaoChi) from BaoChiViet tin ";
        org.hibernate.Query query = session.createQuery(hql);
        ma = (Integer) query.uniqueResult();
        session.close();
        return ma;

    }
}
